<template>
	<view class="container">
		<!-- 顶部签到栏 -->
		<view class="header">
			<view class="sign-in">
				<text>签到</text>
			</view>
			<view class="header-icons">
				<image class="header-icon" src="/static/me/设置.png"></image>
				<image class="header-icon" src="/static/me/消息.png"></image>
			</view>
		</view>

		<!-- 用户信息区域 -->
		<view class="user-card" @click="handleLogin">
			<view class="avatar-section">
				<template v-if="!userInfo.nickName">
					<!-- 未登录状态 -->
					<image class="avatar" src="/static/me/个人.png" mode="aspectFill"></image>
					<text class="register">注册/登录</text>
				</template>
				<template v-else>
					<!-- 已登录状态 -->
					<image class="avatar" :src="userInfo.avatarUrl || '/static/me/个人.png'" mode="aspectFill"></image>
					<view class="username">{{ userInfo.nickName || '微信用户' }}</view>
				</template>
			</view>
			<view class="stats">
				<view class="stat-item" v-for="(item, index) in stats" :key="index">
					<text class="stat-number">{{ item.number }}</text>
					<text class="stat-label">{{ item.label }}</text>
				</view>
			</view>
		</view>

		<!-- 功能列表 -->
		<view class="menu-list">
			<view class="menu-item" v-for="(item, index) in menuItems" :key="index">
				<image class="menu-icon" :src="item.icon"></image>
				<text class="menu-text">{{ item.text }}</text>
				<!-- <image class="arrow" src="/static/icons/arrow-right.png"></image> -->
			</view>
		</view>
		<view class="listBox">
			<u-popup  :show="show" @close="close">
				<view class="popup">
					<view class="title">
						获取用户昵称、头像
					</view>
					<view class="flex">
						<view class="label">
							获取用户头像：
						</view>
						<button class="avatar-warpper" open-type="chooseAvatar" @chooseAvatar="onChooseavatar">
							<img :src="userInfo.avatarUrl" class="acatar" /></button>
					</view>
					<view class="flex">
						<view class="label">
							获取用户昵称:
						</view>
						<input type="text" @input="changeName" placeholder="请输入昵称" />
					</view>
				</view>
			</u-popup >
		</view>
	</view>

</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		login,
		getUserInfo
	} from '../../api/api'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	
	const userInfo = reactive({
		nickName: '',
		avatarUrl: ''
	});
	//控制登录弹出层
	const show = ref(false)

	const close=()=>{
			show.value=false
	}
const onChooseavatar=(e)=>{
	userInfo.avatarUrl=e.detail.avatarUrl
}
const changeName=(e)=>{
	userInfo.nickName=e.detail.value
}
	// 登录处理函数
	const handleLogin = async () => {
		try {
			// 1. 显示授权弹窗
			const modalRes = await uni.showModal({
				title: '温馨提醒',
				content: '亲，授权才能登录',
				confirmText: '去登录',
				cancelText: '暂不'
			})

			if (!modalRes.confirm) return

			// 2. 获取微信code
			const loginRes = await uni.login({
				provider: 'weixin'
			})
			console.log('获取到的微信code:', loginRes.code)

			// 3. 调用登录接口获取token
			const {
				token
			} = await login(loginRes.code)
			uni.setStorageSync('token', token)
			console.log('登录成功，token:', token)

			// 4. 获取用户信息
			const userRes = await getUserInfo()
			console.log('用户信息:', userRes)

			// 5. 更新用户信息
			userInfo.nickName = userRes.nickName || '微信用户'
			userInfo.avatarUrl = userRes.avatarUrl || '/static/me/个人.png'

			uni.showToast({
				title: '登录成功'
			})
			show.value = true

		} catch (err) {
			console.error('登录流程出错:', err)
			uni.showToast({
				title: err.message || '登录失败',
				icon: 'none'
			})
		}
	}

	// 页面加载时检查登录状态
	onLoad(async () => {
		try {
			const token = uni.getStorageSync('token')
			if (token) {
				const userRes = await getUserInfo()
				userInfo.nickName = userRes.nickName
				userInfo.avatarUrl = userRes.avatarUrl
				show.value=true
			}
		} catch (err) {
			console.error('获取用户信息失败:', err)
		}
	})

	const stats = ref([{
			number: "12",
			label: "点赞"
		},
		{
			number: "12",
			label: "喜欢"
		},
		{
			number: "12",
			label: "浏览"
		},
		{
			number: "12",
			label: "收藏"
		}
	])

	const menuItems = ref([{
			text: "个人信息",
			icon: "/static/me/个人.png"
		},
		{
			text: "我的购物车",
			icon: "/static/me/购物车.png"
		},
		{
			text: "用户反馈",
			icon: "/static/me/用户反馈.png"
		},
		{
			text: "我的邮件",
			icon: "/static/me/邮件.png"
		},
		{
			text: "分享有礼",
			icon: "/static/me/礼物.png"
		}
	])
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #f5f5f5;
		position: relative;
		padding: 20rpx;
	}

	.header {
		height: 80rpx;
		background-color: skyblue;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;

		.sign-in {
			color: white;
			font-size: 36rpx;
			font-weight: bold;
		}

		.header-icons {
			display: flex;
			gap: 20rpx;

			.header-icon {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

	.user-card {
		background-color: white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;

		.avatar-section {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;

			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				margin-right: 20rpx;
				background-color: #f0f0f0;
			}

			.username,
			.register {
				font-size: 36rpx;
				font-weight: bold;
			}
		}

		.stats {
			display: flex;
			justify-content: space-around;

			.stat-item {
				display: flex;
				flex-direction: column;
				align-items: center;

				.stat-number {
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.stat-label {
					font-size: 28rpx;
					color: #666;
				}
			}
		}
	}

	.menu-list {
		background-color: white;
		border-radius: 16rpx;

		.menu-item {
			display: flex;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1rpx solid #eee;

			.menu-icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}

			.menu-text {
				flex: 1;
				font-size: 32rpx;
			}

			.arrow {
				width: 24rpx;
				height: 24rpx;
			}
		}
	}
	.popup{
		padding: rpx;
		border-radius: 20rpx 20rpx 0 0;
		 .title{
			 margin-bottom: 20rpx;
			font-size: 40rpx;
			text-align: center;
		 }
		 .flex{
			 display: flex;
			 justify-content: center;
			 border-bottom: 1px solid #f5f5f5;
			 padding: 24rpx 0;
			 .label{
				 position: absolute;
				 left: 0;
			 }
		 }
		 img{
			 width: 70rpx;
			 height: 70rpx;
			 margin-left: 20rpx;
		 }
		 .avatar-warpper{
			 border: none;
			 border-radius: 10rpx;
			 width: 70rpx;
			 height: 70rpx;
			 margin-left: 20rpx;
		 }
	}
</style>